import 'dart:core';
import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:kasax_demos2/entry/entrys.dart';
import 'package:kasax_demos2/widgets/posts_widgets.dart';

import 'sliver_list_demo.dart';

/// sliverappbar: 伸展空间
class SliverAppbarDemo2 extends StatefulWidget {
  const SliverAppbarDemo2({Key? key}) : super(key: key);

  @override
  _SliverAppbarDemo2State createState() => _SliverAppbarDemo2State();
}

// SliverGridDelegateWithFixedCrossAxisCount设置交叉轴上的条目的数量
class _SliverAppbarDemo2State extends State<SliverAppbarDemo2> {
  bool pinned = false;
  bool floating = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SliverAppBarDemoWidget(pinned, floating),
      floatingActionButton: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            FloatingActionButton(
              onPressed: () {
                setState(() {
                  floating = false;
                  pinned = true;
                });
              },
              child: Text("固定"),
            ),
            SizedBox(
              height: 15,
            ),
            FloatingActionButton(
              onPressed: () {
                setState(() {
                  floating = false;
                  pinned = false;
                });
              },
              child: Text("不固定"),
            ),
            SizedBox(
              height: 15,
            ),
            FloatingActionButton(
              onPressed: () {
                floating = true;
                pinned = false;
              },
              child: Text("浮动"),
            ),
            SizedBox(
              height: 15,
            ),
            FloatingActionButton(
              onPressed: () {
                floating = false;
                pinned = false;
              },
              child: Text("不浮动"),
            ),
            SizedBox(
              height: 25,
            ),
          ],
        ),
      ),
    );
  }
}

class SliverAppBarDemoWidget extends StatefulWidget {
  final bool pinned;
  final bool floating;

  const SliverAppBarDemoWidget(this.pinned, this.floating, {Key? key}) : super(key: key);

  @override
  _SliverAppBarDemoWidgetState createState() => _SliverAppBarDemoWidgetState();
}

class _SliverAppBarDemoWidgetState extends State<SliverAppBarDemoWidget> {
  @override
  Widget build(BuildContext context) {
    return buildPostsListWidget();
  }

  PostsListWidget buildPostsListWidget() {
    return PostsListWidget((BuildContext context, AsyncSnapshot<List<Posts>> snapshot) {
      log("PostsListWidget buildPageContent run...");
      return buildCustomScrollView(snapshot);
    });
  }

  CustomScrollView buildCustomScrollView(AsyncSnapshot<List<Posts>> asyncsnapshot) {
    return CustomScrollView(
      slivers: [
        SliverAppBar(
          automaticallyImplyLeading: false, // 隐藏返回箭头
          // title: Text("NingHao"),
          // centerTitle: true,
          // pinned: widget.pinned,
          // 固定位置显示
          floating: true,
          // 浮动显示
          expandedHeight: 178.0,
          // 展开的高度
          flexibleSpace: FlexibleSpaceBar(
            // 伸展空间的Bar
            centerTitle: true,
            title: Text(
              "Ninghao Flutter".toUpperCase(),
              style: TextStyle(fontSize: 15.0, letterSpacing: 3.0, fontWeight: FontWeight.w400),
            ),
            background: Image.network(
              "https://resources.ninghao.net/images/overkill.png",
              fit: BoxFit.cover,
            ),
          ),
        ),
        // 去除顶端的空白
        MediaQuery.removePadding(
          context: context,
          child: SliverSafeArea(
              // 显示在安全区域
              // 添加内边距
              sliver: SliverListDemo(asyncsnapshot.data!)),
          removeTop: true,
        )
      ],
    );
  }
}
